<?php
class Database {
	var $connected = false;
	var $connection = null;
	
	function connect() {
		global $connection, $connected;
		
		try {
			$connection = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
			mysql_select_db(DB_DATABASE, $connection);
			
			if(!$connection) {
				throw new Exception("MySQL error: " . mysql_error());
			} else {
				$connected = true;
			}
		} catch (Exception $ex) {
			$ex->getMessage();
		}
	}
	
	function close() {
		global $connection, $connected;
		
		if($connected) {
			mysql_close($connection);
			$connected = false;
		} else {
			return 'Error: No connection currently active';
		}
	}
	
	/*
	 * DATABASE QUERIES
	 */
	
	function slc_db_all($db_table){
		$query = "select * from $db_table";
		$data = mysql_query($query);
		return $data;
	}
	
	function slc_db_where($db_table, $db_field, $field) {
		$query = "select * from $db_table where $db_field = '$field'";
		$data = mysql_query($query);
		return $data;
	}
	
	function slc_db_top($db_table, $db_field, $field, $x, $y) {
		$query = "select * from $db_table where $db_field = '$field' LIMIT $x,$y";
		$data = mysql_query($query);
		return $data;
	}
}
?>